package com.ikangtai.shecare.common.baseView.pickerDialog;

import android.content.Context;
import android.widget.FrameLayout;
import android.widget.NumberPicker;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.Constant;

public class MyInfoPicker extends FrameLayout {
    public static final String regular[] = {Constant.kRule, Constant.kIrregular};
    private final NumberPicker brandSpinner;
    private int mBrand = 1;
    private OnBrandChangedListener mOnBrandChangedListener;

    public MyInfoPicker(Context context, String dialogName) {
        super(context);

        /**
         * 加载布局
         */
        inflate(context, R.layout.dialog_brand_record, this);
        /**
         * 初始化控件
         */
        brandSpinner = (NumberPicker) this.findViewById(R.id.brand_picker);
        if (dialogName.equals(Constant.MENSES_LENGTH)) {
            brandSpinner.setMaxValue(15);
            brandSpinner.setMinValue(1);
            brandSpinner.setValue(5);
            mBrand = 5;
        } else if (dialogName.equals(Constant.PERIOD_LENGTH)) {
            brandSpinner.setMaxValue(55);
            brandSpinner.setMinValue(5);
            brandSpinner.setValue(28);
            mBrand = 28;
        } else if (dialogName.equals(Constant.MENSES_REGULAR)) {
            brandSpinner.setDisplayedValues(regular);
            brandSpinner.setMaxValue(1);
            brandSpinner.setMinValue(0);
            brandSpinner.setValue(0);
            mBrand = 0;

        }
        brandSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
        brandSpinner.clearFocus();
        brandSpinner.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    }

    private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mBrand = brandSpinner.getValue();
            onTemperatureChanged();
        }
    };

    /*
     *接口回调 参数是当前的View 年月日小时分钟
     */
    public interface OnBrandChangedListener {
        void onTemperatureChanged(MyInfoPicker view, int minute);
    }

    /*
     *对外的公开方法
     */
    public void setOnBrandChangedListener(OnBrandChangedListener callback) {
        mOnBrandChangedListener = callback;
    }

    private void onTemperatureChanged() {
        if (mOnBrandChangedListener != null) {
            mOnBrandChangedListener.onTemperatureChanged(this, mBrand);
        }
    }
}
